%make background  database
YearMonthDay=20200115;
photoStart=1213;
photoEnd=1462;
mode=0;
photoBEC='\\RB87BEC2\';
if strcmp(photoBEC,'\\Camera-PC\')
    photoNamePre='4552test_';
    filename_pre='ascOD4552test_';
    resolution=512;
    folderName=[photoBEC 'ShareFolder\' num2str(YearMonthDay) 'morning\4552\'];
    refimagesIniDirectory='D:\Rb87ProgramBEC3\removeBackground\backgrounddata\refimagesIni_4552new.mat';
elseif strcmp(photoBEC,'\\RB87BEC2\')
    photoNamePre='22860test_';
    filename_pre='ascOD22860test_';
    resolution=1024;
    folderName=[photoBEC 'ShareFolder\' num2str(YearMonthDay) 'morning\ikon-22860\'];
    refimagesIniDirectory='D:\Rb87ProgramBEC3\removeBackground\backgrounddata\refimagesIni_22860new.mat';
end

refNum=250;
refInd=photoStart:(photoStart+refNum-1);
if mode==0
    if exist(refimagesIniDirectory,'file')==2
        refimagesIniStruct=load(refimagesIniDirectory);
        refimagesIni=refimagesIniStruct.refimagesIni;
        
        if size(refimagesIni,3)==refNum
            refimages=refimagesIni;
        else
            error('Error: refNum ~= loaded reference images');
        end
    end
    
    
    refInd=photoStart:min((photoStart+refNum-1),photoEnd);
    refimagesIni(:,:,mod(refInd,refNum)+1)=refCreate(refInd,'bg',folderName,photoNamePre,resolution);
    save(refimagesIniDirectory','refimagesIni');
end